#include <iostream>
using namespace std;

#include "XrdDownloadManagerAppMon.hh"
#include <XrdDownloadManagerLog.hh>

XrdDownloadManagerAppMon::XrdDownloadManagerAppMon(const char *monalisaConfigFile)
{
#ifdef HAS_APPMON
    sprintf(fMonalisaConfigFile, "%s", monalisaConfigFile);
    sprintf(fFarmName, "%s", "XrdDownloadManager");
    sprintf(fJobName, "%s", "");
#endif
}

XrdDownloadManagerAppMon::~XrdDownloadManagerAppMon()
{
#ifdef HAS_APPMON
#endif
}


bool XrdDownloadManagerAppMon::Init(string configFile,string name)
{
#ifdef HAS_APPMON
    if (configFile.empty()) return false;

    sprintf(fMonalisaConfigFile, "%s", configFile.c_str());

    if (name.empty()) return false;

    sprintf(fFarmName, "%s", name.c_str());

    fAppMon = 0;
//   const char* message = "Hello";
//     double val = 1;
    char logmsg[100];

    char *config[1] = {fMonalisaConfigFile};
//   config = (char*)fMonalisaConfigFile.data();
//   sprintf(config[0], fMonalisaConfigFile);
    // Part 1 (optional) get the name that is to be looked up
    char hostname[100];
    gethostname(hostname, 100);

    XrdDMLogDebugSilent ( "fFarmName is %s ",fFarmName);

    sprintf(fJobName, "%s_%d", gethostbyname(hostname)->h_name, getpid());
    XrdDMLogDebugSilent ( "fJobName is %s ",fJobName);
    sprintf(logmsg, "Creating %s\n", fJobName);
    logger(INFO, logmsg);

    try {
        fAppMon = new ApMon(1, config);
        fAppMon->setRecheckInterval(300);
        sleep(1);

    }
    catch (runtime_error &e) {
        logger(WARNING, e.what());
    }
#endif
return true;
}

bool XrdDownloadManagerAppMon::Finish()
{
#ifdef HAS_APPMON
#endif
return true;
}

bool XrdDownloadManagerAppMon::SendInfoMessage(char* message)
{
#ifdef HAS_APPMON
    char logmsg[100];
    try {

        if (fAppMon) {
            char hostname[100];
            gethostname(hostname, 100);
            sprintf(fJobName, "%s_%d", gethostbyname(hostname)->h_name, getpid());
            XrdDMLogDebugSilent ( "fJobName SendInfoMessage i %s ",fJobName);
            sprintf(logmsg, "Sending %s to %s with name %s\n", message, fFarmName, fJobName);
            cout << logmsg << endl;
            logger(INFO, logmsg);
            fAppMon->sendParameter(fFarmName, fJobName , (char*)"Status", 0);
            fAppMon->sendParameter(fFarmName, fJobName , (char*)"Message", message);
            fAppMon->sendParameter(fFarmName, fJobName , (char*)"from", fFileFrom);
            fAppMon->sendParameter(fFarmName, fJobName , (char*)"to", fFileTo);
        }
        else {
            sprintf(logmsg, "fAppMon is null in SendInfoMessage(char* message)");
            logger(INFO, logmsg);
        }
    }
    catch (runtime_error &e) {
        logger(WARNING, e.what());
    }


#endif
return true;
}

bool XrdDownloadManagerAppMon::SendErrorMessage(char* message)
{
#ifdef HAS_APPMON
    char logmsg[100];
    try {
        if (fAppMon) {
            char hostname[100];
            gethostname(hostname, 100);
            sprintf(fJobName, "%s_%d", gethostbyname(hostname)->h_name, getpid());
            XrdDMLogDebugSilent ( "fJobName SendErrorMessage i %s ",fJobName);
            sprintf(logmsg, "Sending %s to %s with name %s\n", message, fFarmName, fJobName);
//             cout <fJobName< logmsg << endl;
            logger(INFO, logmsg);

            fAppMon->sendParameter(fFarmName, fJobName , (char*)"Status", 1);
            fAppMon->sendParameter(fFarmName, fJobName , (char*)"Message", message);
            fAppMon->sendParameter(fFarmName, fJobName , (char*)"from", (char*)fFileFrom);
            fAppMon->sendParameter(fFarmName, fJobName , (char*)"to", (char*)fFileTo);
        }
        else {
            sprintf(logmsg, "fAppMon is null in SendErrorMessage(const char* message)");
            logger(INFO, logmsg);
        }
    }
    catch (runtime_error &e) {
        logger(WARNING, e.what());
    }
#endif
return true;

}


void XrdDownloadManagerAppMon::SetFileFromTo(const char* from, const char* to)
{
#ifdef HAS_APPMON
    sprintf(fFileFrom, "%s", from);
    sprintf(fFileTo, "%s", to);
#endif
}

// kate: indent-mode cstyle; space-indent on; indent-width 0; 
